import $service from '../../../../service.js';
var doctor_id;
const app = getApp();
Page({
  data: {
    year: 0,
    month: 0,
    date: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    isToday: [],
    isTodayWeek: false,
    todayIndex: 0,
    workDate: []
  },
  onLoad: function (e) {
    doctor_id = e.doctor_id
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    this.setData({
      year: year,
      month: month,
      isToday: '' + year + month + now.getDate()
    })
    this.dateInit();
  },
  
  dateInit: function (setYear, setMonth) {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let dateArr = [];                       //需要遍历的日历数组数据
    let arrLen = 0;                         //dateArr的数组长度
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth();                 //没有+1方便后面计算当月总天数
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    let startWeek = new Date((month + 1) + '/' + 1 + '/' + year).getDay(); //目标月1号对应的星期
    let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
    let obj = {};
    let num = 0;
    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: '' + year + '-' + $service.addZero((month + 1)) + '-' + $service.addZero(num),
          dateNum: num,
          weight: 5
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    this.setData({
      dateArr: dateArr
    })
    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
    if (nowYear == getYear && nowMonth == getMonth) {
      this.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      this.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
    this.getDoctorSchedulingListPc();
  },

  // 获取排班
  getDoctorSchedulingListPc() {
    let zero = this.data.month < 10 ? "0" : ''
    let parmas = {
      "doctorId": Number(doctor_id),
      "schedulingDate": this.data.year +'-' + zero  + this.data.month 
    }
    app.http.getDoctorSchedulingListPc(parmas).then(res => {
      res.data.list.forEach((item)=>{
        item.day = this.getDay('day', item.schedulingDate);
        item.date =  this.getDay('date', item.schedulingDate);
        this.data.dateArr.forEach((ite => {
            if (item.schedulingDate == ite.isToday) {
                ite.timeshow = true
            }
        }))
      })
      this.setData({
        workDate: res.data.list,
        dateArr: this.data.dateArr
      })
    })
  },

  /**
   * 上月切换
   */
  lastMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  /**
   * 下月切换
   */
  nextMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  //获取日期
  getDay(type, time) {
    let now = new Date(time);
    if (type === 'day') {
      let day = '';
      switch (now.getDay()) {
        case 0:
            day = "星期天";
            break;
        case 1:
            day = "星期一";
             break;
        case 2:
            day = "星期二";
             break;
        case 3:
            day = "星期三";
             break;
        case 4:
            day = "星期四";
             break;
        case 5:
            day = "星期五";
             break;
        case 6:
            day = "星期六";
      }
      return day
    } else {
      return now.getDate() + '号';
    }
  }
})
